package travelDream.validator;

import java.util.Map;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import org.primefaces.validate.ClientValidator;
  
/** 
 * Custom JSF Validator for Email input 
 */  
@FacesValidator("prime.passwordValidator")  
public class PasswordValidator implements Validator, ClientValidator {  
  
    private Pattern pattern;  
   
    private static final String PASSWORD_PATTERN = "((?=.*\\d)(?=.*[a-z]).{8,32})";
   
    public PasswordValidator() {  
        pattern = Pattern.compile(PASSWORD_PATTERN);  
    }  
  
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {  
        if(value == null) {  
            return;  
        }  
          
        if(!pattern.matcher(value.toString()).matches()) {  
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Validation Error",   
                        value + " is not a valid password;"));  
        }  
    }  
      
    public Map<String, Object> getMetadata() {  
        return null;  
    }  
  
    public String getValidatorId() {  
        return "prime.passwordValidator";  
    }  
      
}  